package com.yueman.ptoolopenticket.ticket.config;


import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.yueman.ptoolopenticket.ticket.utils.Constant;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component(Constant.LIXIN + "config")
@Data
@Slf4j
public class LixinConfig implements InitializingBean {
    public int dev = 1;
    public String url;
    public String version;
    public String client;
    public static final String PRIVATEKEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKtSbeUzEW0VrIh3UgQnh2EnDNb/0Y0pvnI86GCp8w/tlodHQJNY5nr4jFCgsQ7MA8p/4nWeoZA/x4qzaaApUfARlkU5n7iIztZA5b8bgghiVpZuffaOec5JCuY0m18891llby/KQwpuFP22v0LCsAfQwSv1fPRLr4XnOaZa1YK9AgMBAAECgYBmeJwwrhXrOD6FNVFCGN6ZloDQjByroV8+9v1K6/vs9//EFDMkrtTUkptikmq03gQad9lc3q/mFq/kXyF7YZuWzxVCl3QgM6YxI4sQoPHWSgXQzBhfxmIUtPUILcUoFovAEQ9xxwCvUAJzYv6/SViD7Urfgz089J3JLuNG6Y2rBQJBAOigsLAPUGYOBLiG7pPeFxbFAvbTzIrpJUw7Zy0xQE8bAytM9FoWlKIUgwGKS8N76Kim3Y76ce/HjFJiZzXZkT8CQQC8iOxaIgTvSBQuV53U0xs3mMsx9eiwKVRciKtFDm36yUGkr8MB+GUMJGcNxsTnu7fustIgIRTvz3WucD6Y1HEDAkBEYA7vAOFRzecHUenIHqxMrgWsBR8G6XZkDMXL28EaP0dUL99Wp3JynXLQPhT81WvSPpG619QrdUzCXlgV1OhDAkBQUcTfYT7Y8cwnMgx7brRZqy5lym2nHvRht5e/g7Y28SaWEorxe/EQizGcHOImF0L03hnGtlFFGQz5k7gXQ+ohAkEAywuALwpeKbKsthIX6VlahBbf/imqaJhbfgzyQD+Exu/exRfe+jO4nb54UeGnMGB9H7QglcoUDKm+04mdTTCQ8A==";
    public static final String PUBLICKEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrUm3lMxFtFayId1IEJ4dhJwzW/9GNKb5yPOhgqfMP7ZaHR0CTWOZ6+IxQoLEOzAPKf+J1nqGQP8eKs2mgKVHwEZZFOZ+4iM7WQOW/G4IIYlaWbn32jnnOSQrmNJtfPPdZZW8vykMKbhT9tr9CwrAH0MEr9Xz0S6+F5zmmWtWCvQIDAQAB";
    public static final String YMREGISTERKEY = "hS/smEXQ9WDOLF70iXi9a4c8IlBdou/uNPZGTIKOtT3agyoeNUwNy7EDfRJkn8gQeMbjIhw6zUGvMT5GtUmZD6BR/9pM8oMVnZ+X1Jzun58VJIXT+OXdE8aFGRog/2StooWevjR45EjYzoDCZtzs1rrjhVzgluh6dwt81OQJc0I=";
    public String mainName = "上海灵才科技有限公司";
    public String mainAcct = "217";
    public String nsrbh = "";

    @Override
    public void afterPropertiesSet() throws Exception {
        switch (dev) {
            case 1:
                url = "http://47.104.98.114:1006/entrance/do";
                client = "android";
                version = "v1.0";
                nsrbh = "91310106MA1FY4YR6K";
                break;
            case 2:
                break;
            default:
                break;
        }
    }

    @SneakyThrows
    public Map<String, String> postdata(Map<String, Object> mapContent) {
        String decrypt = SecureUtil.rsa(PRIVATEKEY, PUBLICKEY).decryptStr(YMREGISTERKEY, KeyType.PublicKey);
        Map<String, Object> map = new HashMap<>();
        map.put("ywlx", mapContent.get("ywlx"));
        map.put("userId", mapContent.get("userId"));
        map.put("nsrbh", nsrbh);
        if (mapContent.containsKey("sssf")) {
            map.put("sssf", mapContent.get("sssf"));
            mapContent.remove("sssf");
        }
        mapContent.remove("ywlx");
        mapContent.remove("userId");
        map.put("content", Base64.encode(SecureUtil.aes(decrypt.getBytes()).encrypt(JSONUtil.toJsonStr(mapContent))));
        String s = JSON.toJSONString(map);
        log.info("请求地址：{}", url);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(s);
        Map<String, Object> rtmap = new HashMap<>();
        rtmap.put("sendMsg", s);
        String body = HttpUtil.post(url, rtmap);
        String result = new String(SecureUtil.aes(decrypt.getBytes()).decrypt(body));
        return JSONUtil.toBean(result, Map.class);
    }

    public static void main(String[] args)  {
       /* String decrypt = SecureUtil.rsa(PRIVATEKEY, PUBLICKEY).decryptStr(YMREGISTERKEY, KeyType.PublicKey);
        Map map = new HashMap<>();
        map.put("ywlx", "sqypt.check.version");
        map.put("userId", "");
        Map mapContent = new HashMap();
        mapContent.put("version", "v1.0");
        mapContent.put("client", "android");
        map.put("content", Base64.encode(AesUtil.encrypt(JSON.toJSONString(mapContent), decrypt)));
        map.put("nsrsbh", "91310106MA1FY4YR6K");
        String s = JSONUtil.toJsonStr(map);
        String url = "http://47.104.98.114:1006/entrance/do";
        Map<String,String> jMap=new HashMap<>();
        jMap.put("sendMsg",s);
        JSONArray jsonArray=new JSONArray();
        jsonArray.add(jMap);
        String body = HttpUtil.post(url, JSONUtil.toJsonStr(jsonArray));
        System.out.println("body:"+body);
        String result = SecureUtil.aes(decrypt.getBytes()).decryptStr(body);
        System.out.println("解密报文为：" + result);*/
    }
}
